package com.bcu.cn.onlineexamsystem.studentmanage.service.impl;

import com.bcu.cn.onlineexamsystem.studentmanage.controller.StudentProfileDTO;
import com.bcu.cn.onlineexamsystem.studentmanage.mapper.StudentProfileMapper;
import com.bcu.cn.onlineexamsystem.studentmanage.service.StudentProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentProfileServiceImpl implements StudentProfileService {
    
    @Autowired
    private StudentProfileMapper studentProfileMapper;
    
    @Override
    public StudentProfileDTO getStudentProfile(String userId) {
        try {
            StudentProfileDTO profile = studentProfileMapper.getStudentProfile(userId);
            if (profile != null) {
                // 确保空字符串显示为"未设置"
                if (profile.getEmail() == null || profile.getEmail().trim().isEmpty()) {
                    profile.setEmail("未设置");
                }
                if (profile.getPhone() == null || profile.getPhone().trim().isEmpty()) {
                    profile.setPhone("未设置");
                }
                if (profile.getClassName() == null || profile.getClassName().trim().isEmpty()) {
                    profile.setClassName("未分配");
                }
                if (profile.getCourseNames() == null || profile.getCourseNames().trim().isEmpty()) {
                    profile.setCourseNames("暂无课程");
                }
            }
            return profile;
        } catch (Exception e) {
            System.err.println("获取学生个人信息失败: " + e.getMessage());
            return null;
        }
    }
    
    @Override
    public boolean updateEmail(String userId, String email) {
        try {
            // 邮箱格式验证
            if (email == null || email.trim().isEmpty()) {
                System.err.println("邮箱不能为空");
                return false;
            }
            
            if (!email.matches("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$")) {
                System.err.println("邮箱格式不正确: " + email);
                return false;
            }
            
            int result = studentProfileMapper.updateEmail(userId, email.trim());
            boolean success = result > 0;
            System.out.println("更新邮箱结果: userId=" + userId + ", email=" + email + ", result=" + result + ", success=" + success);
            return success;
        } catch (Exception e) {
            System.err.println("更新邮箱异常: " + e.getMessage());
            return false;
        }
    }
    
    @Override
    public boolean updatePhone(String userId, String phone) {
        try {
            // 手机号格式验证（中国大陆手机号）
            if (phone == null || phone.trim().isEmpty()) {
                System.err.println("手机号不能为空");
                return false;
            }
            
            if (!phone.matches("^1[3-9]\\d{9}$")) {
                System.err.println("手机号格式不正确: " + phone);
                return false;
            }
            
            int result = studentProfileMapper.updatePhone(userId, phone.trim());
            boolean success = result > 0;
            System.out.println("更新手机号结果: userId=" + userId + ", phone=" + phone + ", result=" + result + ", success=" + success);
            return success;
        } catch (Exception e) {
            System.err.println("更新手机号异常: " + e.getMessage());
            return false;
        }
    }
} 